﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IndyMosaic.Models.Syndication;

namespace IndyMosaic.Models.Blog
{
    
    public partial class Entry : Validatable, ISyndicationItem
    {
        
        public string MoreTag
        {
            get
            {
                return "<##MORE##>";
            }
        }
        #region Properties
        public bool HasMoreTag 
        {
            get
            {
                return Content.Contains(MoreTag);
            }
        }
        public IQueryable<Comment> ApprovedComments
        {
            get
            {
                return this.Comments.Where(c => c.ApprovalStatus == ApprovalStatus.Approved).AsQueryable();
            }
        }
        #endregion
        partial void OnCreated()
        {
            this.AuthorName = string.Empty;
            this.Content = string.Empty;
            this.Title = string.Empty;
            this.UserID = Guid.Empty;
            
        }
        partial void OnValidate(System.Data.Linq.ChangeAction action)
        {
            if (!IsValid)
            {
                throw new ApplicationException("Rule violations prevent saving");
            }
        }
        public override IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (string.IsNullOrEmpty(Title))
            {
                yield return new RuleViolation("Title is required", "Title");
            }
            if (string.IsNullOrEmpty(AuthorName))
            {
                yield return new RuleViolation("Author's Name is required", "AuthorName");
            }
            if (UserID == Guid.Empty)
            {
                yield return new RuleViolation("User ID is required", "UserID");
            }
            if (string.IsNullOrEmpty(Content))
            {
                yield return new RuleViolation("Some content is required", "Content");
            }

        }

        public string GetSummary()
        {
            int index = Content.IndexOf(MoreTag);
            int maxSummarySize = 300;
            if (Content.Length > maxSummarySize && index > 0)
            {
                return Content.Substring(0, index);
            }
            else
            {
                return Content;
            }
        }

        #region ISyndicationItem Members

        public long EnclosureLength
        {
            get
            {
                return 0;
            }
        }
        //public string EnclosureURL
        //{
        //    get
        //    {
        //        return null;
        //    }
        //}

        public string GetEnclosureURL(System.Web.Mvc.UrlHelper urlHelper)
        {
            return null;
        }

        #endregion
    }
}
